<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

use app\common\model\CoinAddress;

/**
 * wallet逻辑
 */
class Wallet extends LogicBase
{
    

    
    /**
     * 获取wallet列表
     */
    public function getwalletList($where = [], $field = 'a.*', $order = '')
    {
        
        $this->modelwallet->alias('a');
        

        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $where['a.is_currency']=2;
        $where['a.is_virtual']=1;
        
        return $this->modelwallet->getList($where, $field, $order);
    }

    /**
     * 获取wallet_address列表
     */
    public function getwalletAddressList($where = [], $field = 'a.*', $order = '')
    {
        
        $this->modelCoinAddress->alias('a');



        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $count= $this->modelCoinAddress->alias('a')->where($where)->count();
        
        return ['list'=>$this->modelCoinAddress->alias('a')->field($field)->where($where)->order($order)->paginate(20),'count'=>$count];
    }


    
    /**
     * 获取wallet列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * wallet信息编辑
     */
    public function walletEdit($data = [])
    {
        
        $validate_result = $this->validatewallet->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatewallet->getError()];
        }
        
        $url = url('walletList');


        
        $result = $this->modelwallet->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, 'wallet' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, 'wallet操作成功', $url] : [RESULT_ERROR, $this->modelwallet->getError()];
    }

    /**
     * 获取wallet信息
     */
    public function getwalletInfo($where = [], $field = 'a.*')
    {
        
        $this->modelwallet->alias('a');
        

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $where['a.is_currency']=2;
        $where['a.is_virtual']=1;
        
        return $this->modelwallet->getInfo($where, $field);
    }
    


    /**

    
    /**
     * wallet删除
     */
    public function walletDel($where = [])
    {
        
        $result = $this->modelwallet->deleteInfo($where);
        
        $result && action_log('删除', 'wallet删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, 'wallet删除成功'] : [RESULT_ERROR, $this->modelwallet->getError()];
    }



    /**


    /**
     * wallet添加备用地址
     */
    public function wallet_address($data = [])
    {



        $url=COIN_URL.'/api/v1/address/create';
        
        $account = md5(uniqid(md5(microtime(true)),true));
        $nonce_str = rand(1000,9999);

        switch ($data['mid']){
            case 6:
                $post_data = array(
                    'code' => 'ETH',
                    'account' =>$account,
                    'nonce_str' => $nonce_str ,
                );
                break;
            case 7:
                $post_data = array(
                    'code' => 'BTC',
                    'account' =>$account,
                    'nonce_str' => $nonce_str ,
                );
                break;
            case 8:
                $post_data = array(
                    'code' => 'USDT',
                    'account' =>$account,
                    
                    'nonce_str' => $nonce_str ,
                );
                break;
            default:
                return  [RESULT_ERROR, '不支持该钱包地址创建'];
                break;
        }
        $post_data['filter'] ='geth';
        $sign = md5(asc_sort($post_data).'&key='."79ba9e912e708a0a3177df0641be1e5a");

        $post_data['sign'] = $sign;

        $dat=json_decode(mycurl($url,$post_data,1),true);

        if($dat['status']==1){
            CoinAddress::create([
                'mid'=>$data['mid'],
                'address'=>$dat['data'],
                'type' => $data['type'],
                'label'=>$account
            ]);
        }else{
            return  [RESULT_ERROR, $dat['msg']];
        }


        return  [RESULT_SUCCESS, 'wallet_address添加成功'];
    }


}
